package shuiyuqing.network;

import android.util.Log;

import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.StringRequest;
import com.whu.administrator.ws.MyApplication;
import com.whu.administrator.ws.R;
import com.whu.administrator.ws.VolleySingleton;

import java.io.UnsupportedEncodingException;

import inter.Delegate;
import shuiyuqing.dto.DayWeather;

/**
 * 获取天气
 * Created by Administrator on 2016/3/22.
 */
public class GetWeather  {
    Delegate delegate;
    StringRequest stringRequest;

    public void setDelegate(Delegate delegate) {
        this.delegate = delegate;
    }

    public void get() {
        String url = "http://www.weather.com.cn/adat/sk/101200807.html";
        stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.i("天气信息", response);
                if (delegate != null)
                    delegate.onResponse(response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                if (delegate != null)
                    delegate.onErrorResponse(error);
            }
        }){
            @Override
            protected Response<String> parseNetworkResponse(NetworkResponse response) {
                String temp = null;
                try {
                    temp = new String(response.data, "utf-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                return Response.success(temp, HttpHeaderParser.parseCacheHeaders(response));
            }
        };
        VolleySingleton.getVolleySingleton(MyApplication.share()).addToRequestQueue(stringRequest);
    }

    public void cancel()
    {
        if (!stringRequest.isCanceled())
            stringRequest.cancel();
    }

    public static class ResponseWeather
    {
        DayWeather weatherinfo;

        public DayWeather getWeatherinfo() {
            return weatherinfo;
        }

        public void setWeatherinfo(DayWeather weatherinfo) {
            this.weatherinfo = weatherinfo;
        }
    }
}
